package com.cloud.aiservice.config;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;

@Configuration
public class DatabaseConfig {

		/**
		 * 配置数据源
		 * 
		 */
		@Bean(name = "master")
		@ConfigurationProperties(prefix = "spring.datasource.hikari.master")
		public DataSource master() {
			return DataSourceBuilder.create().build();
		}

		@Bean(name = "slave")
		@ConfigurationProperties(prefix = "spring.datasource.hikari.slave")
		public DataSource slave() {
			return DataSourceBuilder.create().build();
		}
	    @Primary
	    @Bean(name = "dynamicDataSource")
	    public DynamicDataSource dataSource(@Qualifier("master") DataSource master,
	                                        @Qualifier("slave") DataSource slave) {
	        Map<Object, Object> targetDataSource = new HashMap<>();
	        targetDataSource.put(DynamicDataSourceHolder.DB_MASTER, master);
	        targetDataSource.put(DynamicDataSourceHolder.DB_SLAVE, slave);
	        DynamicDataSource dataSource = new DynamicDataSource();
	        dataSource.setTargetDataSources(targetDataSource);
	        return dataSource;
	    }
	    //添加MybatisPlus分页功能
	    @Bean
	    public MybatisPlusInterceptor paginationInterceptor() {
	        return new MybatisPlusInterceptor();
	    }

	    
	    @Bean(name = "SqlSessionFactory")
	    public SqlSessionFactory multiSqlSessionFactory()
	            throws Exception {
	        //配置mybatis,对应mybatis-config.xml
	        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
	        //懒加载
	        LazyConnectionDataSourceProxy p=new LazyConnectionDataSourceProxy();
	        p.setTargetDataSource(dataSource(master(),slave()));
	        sqlSessionFactory.setDataSource(p);
	        //需要mapper文件时加入扫描，sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/*/*Mapper.xml"));
	        MybatisConfiguration configuration = new MybatisConfiguration();
	        configuration.setJdbcTypeForNull(JdbcType.NULL);
	        configuration.setMapUnderscoreToCamelCase(true);
	        configuration.setUseGeneratedKeys(true);
	        configuration.setCacheEnabled(false);
	        sqlSessionFactory.setConfiguration(configuration);
	        //加入上面的两个拦截器
	        Interceptor interceptor[]={paginationInterceptor()};
	        sqlSessionFactory.setPlugins(interceptor);
	        return sqlSessionFactory.getObject();
	    }
}
